package me.helllp.bootman.common.convert;

import java.util.Map;
import java.util.WeakHashMap;

import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;

import me.helllp.bootman.common.base.IBaseEnum;

/**
 * Enums类的转换工厂类，需要在WebMvcConfigurerAdapter中进行配置
 * 
 * @author 虎头
 *
 */
public class EnumsConverterFactory implements ConverterFactory<String, IBaseEnum<?>> {

	@SuppressWarnings("rawtypes")
	private static final Map<Class, Converter> converterMap = new WeakHashMap<>();  
	
	@Override
	public <T extends IBaseEnum<?>> Converter<String, T> getConverter(Class<T> targetType) {
        @SuppressWarnings("unchecked")
		Converter<String, T> result = converterMap.get(targetType);  
        if(result == null) {  
            result = new IntegerStrToEnum<T>(targetType);  
            converterMap.put(targetType, result);  
        }  
        return result;  
	}

}
